package xin.marcher.module.inventory.manage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import xin.marcher.module.inventory.domain.bo.InventoryBucketOperateBO;
import xin.marcher.module.inventory.service.InventoryBucketService;

import java.util.concurrent.BlockingQueue;

/**
 * 多线程消费队列内的数据
 */
@Slf4j
public class OperateClearRunner implements Runnable {
    /**
     * 处理任务的队列
     */
    private BlockingQueue blockingQueue;

    private InventoryBucketService inventoryBucketService;

    public OperateClearRunner(BlockingQueue blockingQueue, InventoryBucketService inventoryBucketService) {
        this.blockingQueue = blockingQueue;
        this.inventoryBucketService = inventoryBucketService;
    }

    /**
     * 内部线程处理每个队列的数据
     */
    @Override
    public void run() {
        while (true) {
            try {
                if (CollectionUtils.isEmpty(blockingQueue)) {
                    Thread.sleep(500);
                    continue;
                }
                InventoryBucketOperateBO operate = (InventoryBucketOperateBO) blockingQueue.take();
                inventoryBucketService.clearBucketOperate(operate);
            } catch (Exception e) {
                log.error("处理分桶任务异常", e);
            }
        }
    }


}
